package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author jishushen
 * @create 2021-02-18 12:12
 * 系统管理之部门管理
 */
@Api(value = "部门资源",description = "部门资源控制器")
@Controller
@Slf4j
@RequestMapping("/department")
public class DepartmentController {
    private static final Logger logger = LoggerFactory.getLogger(DepartmentController.class);
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "测试功能",notes = "其实就是测试好不好使")
    @GetMapping("/test")
    @RequiresPermissions(value = {"test:list","飞梦列表"},logical = Logical.OR)
    @ResponseBody
    public Object test(){
        log.info("hello");
        log.error("world");
        return departmentService.selectAll();
    }

    @ApiOperation(value = "显示列表",notes = "显示所有的部门列表")
    @RequestMapping("/list")
    @RequiresPermissions(value = {"department:list","部门列表"},logical = Logical.OR)
    public String list(Model model, QueryObject qo){
        logger.info("显示所有的部门列表");
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list"; // 找视图 /WEB-INF/views/department/list.ftl
    }

    @ApiOperation(value = "保存/更新功能",notes = "当Department对象中id有值就是更新，没有值就是保存")
    @ApiResponses({
            @ApiResponse(code=200,message="部门保存/更新成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(value = "部门实体",name = "department", paramType = "body")
    })
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
        JsonResult jr = new JsonResult();
        try {
            if(department.getId() == null){ // 新增
                departmentService.insert(department);
            }else {
                departmentService.updateByPrimaryKey(department); // 修改
            }
            jr.setFlag(true);
            jr.setMsg("保存成功");
        } catch (Exception e) {
            jr.setFlag(false);
            jr.setMsg("保存失败");
        }
        return jr;
    }

    @ApiOperation(value = "删除功能",notes = "当部门id进行删除")
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(value = "部门id",
                    name = "id",
                    dataType = "Long",
                    required = true,
                    example = "0"
            )
    })
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list.do"; // 让浏览器重新发一次请求
    }

}
